package com.android.accessibility.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.DownloadListener;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.accessibility.R;
import com.android.accessibility.base.BaseFragment;
import com.android.accessibility.utils.AppConfig;

public class DiscoverFragment extends BaseFragment implements DownloadListener {

    private static final String DEFAULT = "http://temai.m.taobao.com/cheap.htm?pt=1&pid=mm_42561665_15534534_59614694";
    private WebView mWebView = null;
    private TextView mTitleView;
    private ProgressBar mProgressBar;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.discover_layout, null);
        mWebView = (WebView) view.findViewById(R.id.webview);
        mTitleView = (TextView) view.findViewById(R.id.title);
        AppConfig config = AppConfig.get(getActivity());
        if (config != null && !TextUtils.isEmpty(config.getDiscoverTitle())) {
            mTitleView.setText(config.getDiscoverTitle());
        } else {
            mTitleView.setText("发现");
        }
        mProgressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
        mProgressBar.setVisibility(View.GONE);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(mWebViewClient);
        mWebView.setWebChromeClient(mWebChromeClient);
        mWebView.setDownloadListener(this);
        String url = config.getDiscoverUrl();
        if (TextUtils.isEmpty(url)) {
            url = DEFAULT;
        }
        final String loadUrl = url;
        mWebView.postDelayed(new Runnable() {
            @Override
            public void run() {
                mWebView.loadUrl(loadUrl);
            }
        }, 500);
        return view;
    }

    private WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            mProgressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            mProgressBar.setVisibility(View.GONE);
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler,
                SslError error) {
            handler.proceed();
        }
    };

    private WebChromeClient mWebChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            mProgressBar.setProgress(newProgress);
        }
    };

    public boolean onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return false;
    }

    @Override
    public void onDownloadStart(String url, String userAgent,
            String contentDisposition, String mimetype, long contentLength) {
        Uri uri = Uri.parse(url);   
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);  
    }
}
